<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="css/index.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css"> <!-- Font Awesome -->
    <title>{{.title}}</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f9f9f9;
            margin: 0;
            padding: 0;
            display: flex;
            flex-direction: column;
            align-items: center;
            justify-content: center;
            height: 100vh;
        }
        header {
            background-color: #62b0f0;
            width: 100%;
            text-align: center;
            padding: 1rem 0;
            color: white;
        }
        main {
            width: 80%;
            margin: 2rem auto;
            background-color: white;
            padding: 2rem;
            box-shadow: 0 0 10px rgba(0,0,0,0.1);
            border-radius: 8px;
        }
        .shares-table table {
            width: 100%;
            border-collapse: collapse;
        }
        .shares-table th, .shares-table td {
            padding: 12px;
            border: 1px solid #ddd;
            text-align: left;
        }
        .shares-table th {
            background-color: #f2f2f2;
            font-weight: bold;
        }
        .shares-table tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        .shares-table tr:hover {
            background-color: #f1f1f1;
        }
        .actions {
            margin-top: 1rem;
            text-align: center;
        }
        .actions a {
            text-decoration: none;
            color: white;
            background-color: #007bff;
            padding: 10px 20px;
            border-radius: 5px;
        }
        .actions a:hover {
            background-color: #45a049;
        }
        h1 {
            margin: 0;
            font-size: 2.5em;
            color: rgb(0, 0, 0); /* 设置 h1 颜色 */
        }
        .delete-button {
            background-color: #e74c3c;
            color: white;
            border: none;
            padding: 5px 10px;
            border-radius: 5px;
            cursor: pointer;
        }
        .delete-button:hover {
            background-color: #c0392b;
        }
    </style>
    <script>
        document.addEventListener('DOMContentLoaded', function() {
            var modal = document.getElementById("deleteModal");
            var closeModal = document.getElementsByClassName("close")[0];
            var confirmDeleteBtn = document.getElementById("confirmDelete");
            var cancelBtn = document.getElementsByClassName("cancel")[0];
            var modalMessage = document.getElementById("modalMessage");
            var currentRow = null;

            function openModal(message, showCancel = true) {
                modalMessage.textContent = message;
                modal.style.display = "block";
                if (!showCancel) {
                    cancelBtn.style.display = "none";
                } else {
                    cancelBtn.style.display = "inline-block";
                }
            }

            function closeModalFunction() {
                modal.style.display = "none";
                confirmDeleteBtn.onclick = null;
            }

            function deleteShare(shareId, row) {
                fetch('/share/delete', {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json',
                    },
                    body: JSON.stringify({ id: shareId }),
                })
                .then(response => {
                    if (!response.ok) {
                        throw new Error('网络响应不正常');
                    }
                    row.remove();
                    openModal("分享已成功删除！", false);
                    confirmDeleteBtn.onclick = function() {
                        closeModalFunction();
                        location.reload(); // 添加这一行来刷新页面
                    };
                })
                .catch(error => {
                    console.error('请求过程中出现问题:', error);
                    openModal("删除分享失败，请重试！");
                });
            }

            var deleteButtons = document.querySelectorAll('.delete-button');
            deleteButtons.forEach(button => {
                button.addEventListener('click', function(event) {
                    event.preventDefault();
                    currentRow = button.parentElement.parentElement;
                    var shareId = currentRow.getAttribute('data-id');

                    openModal("确认删除该分享吗？");

                    confirmDeleteBtn.onclick = function() {
                        deleteShare(shareId, currentRow);
                    };
                });
            });

            closeModal.onclick = function() {
                closeModalFunction();
            };

            cancelBtn.onclick = function() {
                closeModalFunction();
            };

            window.onclick = function(event) {
                if (event.target == modal) {
                    closeModalFunction();
                }
            };
        });
    </script>
</head>
<body class="container">
<header>
    <h1 class="title">{{.title}}</h1>
</header>
<main>
    <section class="shares-table">
        <table>
            <thead>
            <tr>
                <th>序号</th>
                <th>ID</th> 
                <th>姓名</th>
                <th>分享链接</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            {{range $index, $value := .shares}}
            <tr data-id="{{$value.ID}}">
                <td>{{inc $index}}</td>
                <td>{{$value.ID}}</td>
                <td>{{$value.Name}}</td>
                <td><a href="{{$value.Link}}" target="_blank">{{$value.Link}}</a></td>
                <td>
                    <button class="delete-button">
                        <i class="fas fa-trash-alt"></i>
                    </button>
                </td>
            </tr>
            {{end}}
            </tbody>
        </table>
    </section>
    <div class="actions">
        <a href="/index">返回首页</a>
    </div>

    <div id="deleteModal" class="modal">
        <div class="modal-content">
            <div class="modal-header">
                <span class="close">&times;</span>
            </div>
            <div class="modal-body">
                <p id="modalMessage"></p>
            </div>
            <div class="modal-footer">
                <button class="cancel">取消</button>
                <button class="confirm" id="confirmDelete">确认</button>
            </div>
        </div>
    </div>
</main>
</body>
</html>
